package com.saibo.spider.gather;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.saibo.spider.commons.SubmitRedisCommons;
import com.saibo.spider.pojo.SinaBlogUserCard;
import com.saibo.spider.utils.FileUtil;
import com.saibo.spider.utils.HttpRequestUtil;
import com.saibo.spider.utils.HttpResponseUtil;
import com.saibo.spider.utils.MD5Util;
import com.saibo.spider.utils.StringUtil;


public class MouldSinaUserCard {

	public static void main(String[] args) {
		SinaBlogUserCard usercard = new MouldSinaUserCard()
				.getSinaUserCard("http://weibo.com/aj/v6/user/newcard?ajwvr=6&id=6084440520&type=0&refer_flag=");
		System.out.println(StringUtil.objectToJson(usercard));
		List<SinaBlogUserCard> ls = new ArrayList<SinaBlogUserCard>();
		ls.add(usercard);
		//SubmitRedisCommons.submitRedisData("sina_blog_usercard", StringUtil.objectToJson(ls));
		Map<String,String> mso = new HashMap<String,String>();
		mso.put("jsonData", StringUtil.objectToJson(usercard));
		//System.out.println(HttpResponseUtil.getHtml(HttpRequestUtil.postRequestMethod("http://192.168.1.177:8080/stream/dataSubmit/addData", null, mso)));
	}

	/*
	 * 能拿到的信息 用户头像 认证信息 名称 性别 会员 简介 关注 粉丝 微博 位置 公司
	 * 
	 * 
	 */
	public SinaBlogUserCard getSinaUserCard(String url) {

		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, getRequestHeader());
		String html = HttpResponseUtil.getHtml(httpResponse);
		html = html.replaceAll("try\\{\\(", "");
		html = html.replaceAll("\\)\\}catch\\(e\\)\\{\\};", "");
		Map<String, Object> cache = StringUtil.jsonToMap(html);
		if (cache.get("code").toString().equals("100000") || cache.get("msg").toString().equals("ok")) {
			SinaBlogUserCard userCard = new SinaBlogUserCard();
			userCard.setWeibo_id(getWeiboId(url));
			
			Document document = Jsoup.parse(cache.get("data").toString());
			Element element = document.select("div>div.nc_head").first();
			Element user_img_element = element.select("div>div.pic_box>a>img").first();// 获取用户头像
			System.out.println("头像:" + user_img_element.attr("src"));
			userCard.setUser_img(user_img_element.attr("src"));
			if (element.select("div>div.pic_box>a").size() > 1) {
				String atten = element.select("div>div.pic_box>a>i")
						.attr("title");
				System.out.println("认证信息:" + atten);
				userCard.setAuthentication_msg(atten);
				String attenType = element.select("div>div.pic_box>a>i").attr("class");// 认证类型
				if (attenType.contains("icon_pf_approve_gold")) {
					System.out.println("黄V");
					userCard.setAuthentication(1);
				}
				if (attenType.contains("icon_pf_approve_co")) {
					System.out.println("蓝V");
					userCard.setAuthentication(2);
				}
			}
			// 采集用户名
			Element user_nameElee = element.select("div>div.mask>div.name").first();// 获取用户头像
			Element user_name_element = user_nameElee.select("div>a").first();
			String username = user_name_element.attr("title");
			String userIndexUrl = user_name_element.attr("href");
			String userIndex = getRealUserIndexUrl(userIndexUrl);
			System.out.println("用户昵称:" + username + "\t主页地址:" + userIndex);
			userCard.setRowkey(MD5Util.GET16BITMD5(filterUrl(userIndex)));
			userCard.setNickname(username);
			userCard.setUrl(userIndex);
			// 性别
			Element userSexEle = user_nameElee.select("div>em").first();
			String sexEmClass = userSexEle.attr("class");
			if (sexEmClass.contains("female")) {
				System.out.println("性别:女");
				userCard.setSex(0);
			} else if (sexEmClass.contains("male")) {
				System.out.println("性别:男");
				userCard.setSex(1);
			} else {
				System.out.println("未知");
				userCard.setSex(2);
			}
			// 是否是会员
			if (user_nameElee.select("div>a[href=http://vip.weibo.com/personal?from=main]").size() > 0) {
				System.out.println("会员");
				userCard.setIsvip(1);
			}
			// 简介
			if (element.select("div>div.mask>div.intro").size() > 0) {
				System.out.println("简介:" + element.select("div>div.mask>div.intro>span").attr("title"));
				userCard.setBrief(element.select("div>div.mask>div.intro>span").attr("title"));
			}
			// 粉丝关注发博
			Element div_nc_content = document.select("div>div.nc_content").first();
			Element div_c_count = div_nc_content.select("div>div.c_count").first();
			Element atten_Element = div_c_count.select("div>span.c_follow>a>em").first();
			String atten_str = StringUtil.element2text(atten_Element);// 关注数
			Element fans_Element = div_c_count.select("div>span.c_fans>a>em").first();
			Element weibo_Element = div_c_count.select("div>span.c_weibo>a>em").first();
			String fans_str = StringUtil.element2text(fans_Element);// 关注数
			String weibo_str = StringUtil.element2text(weibo_Element);// 关注数
			System.out.println("关注数:" + getRealNum(atten_str) + "\t粉丝数:" + getRealNum(fans_str) + "\t微博数:"
					+ getRealNum(weibo_str));
			userCard.setAttention_num(getRealNum(atten_str));
			userCard.setFans_num(getRealNum(fans_str));
			userCard.setWeibo_num(getRealNum(weibo_str));
			// 其他信息
			Element div_user_info = div_nc_content.select("div>div.user_info").first();
			Element area_ele = div_user_info.select("div>ul>li>a.interval").first();
			String area_str = area_ele.attr("title");
			if (div_user_info.select("div>ul>li>a.detail").size() > 0) {
				Elements other_info = div_user_info.select("div>ul>li>a.detail");
				for (Element element2 : other_info) {
					String a_href = element2.attr("href");
					if (a_href.contains("school")) {
						String school_str = element2.attr("title");
						System.out.println("学校:" + school_str);
						userCard.setGraduated(school_str);
					}
					if (a_href.contains("work")) {
						String work_str = element2.attr("title");
						System.out.println("就职于:" + work_str);
						userCard.setWork(work_str);
					}

				}
			}
			System.out.println("地区:" + area_str);
			userCard.setArea(area_str);
			return userCard;
		}
		return null;
	}

	/**
	 * 过滤数字杂值
	 * 
	 * @param num
	 * @return
	 */
	public int getRealNum(String num) {
		num = num.replaceAll(" ", "");
		num = num.replaceAll("	", "");
		num = num.replaceAll(" ", "");
		num = num.replaceAll("-", "");
		if (num.contains("万")) {
			num = num.replaceAll("万", "");
			return Integer.parseInt(num) * 10000;
		} else {
			return Integer.parseInt(num);
		}
	}

	/**
	 * 去除携带参数的url杂值，保证生成Rowkey的质量
	 * 
	 * @param url
	 * @return
	 */
	public static String getRealUserIndexUrl(String url) {
		String returnUrl = url;
		if (url.contains("?")) {
			String ca_url[] = url.split("\\?");
			returnUrl = ca_url[0];
		}
		return returnUrl;
	}

	/**
	 * 根据获取usercard的url获取到这个用户的id
	 * @param url
	 * @return
	 */
	public String getWeiboId(String url) {
		try {
			String[] wbId = url.split("&");
			for (String string : wbId) {
				if (string.startsWith("id=")) {
					String weiboId = string.replaceAll("id=", "");
					return weiboId;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
		return "0";
	}
	
	/**
	 * 
	 */
	public String filterUrl(String url){
		if(url.contains("?")){
			String[] surl = url.split("\\?");
			return surl[0];
		}
		return url;
	}

	/**
	 * 获取Usercard请求头
	 * 
	 * @return
	 */
	public static Map<String, String> getRequestHeader() {
		Map<String, String> requestHeader = new HashMap<String, String>();
		requestHeader.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		requestHeader.put("Accept-Encoding", "gzip, deflate, sdch");
		requestHeader.put("Accept-Language", "zh-CN,zh;q=0.8");
		requestHeader.put("Cache-Control", "max-age=0");
		requestHeader.put("Connection", "keep-alive");
		String cookie = FileUtil.txt2String(new File("C:/sinalogincookie.txt"));// 读取保存在本地的已经登陆的Cookie信息
		requestHeader.put("Cookie", cookie);
		requestHeader.put("Host", "weibo.com");
		requestHeader.put("Upgrade-Insecure-Requests", "1");
		requestHeader.put("User-Agent",
				"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36");
		return requestHeader;
	}
}
